/**
 * 时间戳转时间字符串
 * @param timestamp
 * @param format 可用字符串：YmdHis，每个字符中间可用一个其他字符分隔
 * @returns {string}
 */
var dateTime = function(timestamp, format) {
    if(!format) {
        format = 'Y-m-d H:i:s';
    }

    var date = new Date(timestamp * 1000);
    var year = date.getFullYear()
    var month = date.getMonth() + 1
    var day = date.getDate()
    var hour = date.getHours()
    var minute = date.getMinutes()
    var second = date.getSeconds()

    var pattern = ['Y', 'm', 'd', 'H', 'i', 's'];
    var replace = [year, month, day, hour, minute, second].map(formatNumber);
    for(var i=0; i< pattern.length; i++) {
        format = format.replace(pattern[i], replace[i]);
    }
    return format;
}

const formatNumber = function(n) {
    n = n.toString()
    return n[1] ? n : '0' + n
}

module.exports = {
    dateTime: dateTime,
    formatNumber: formatNumber
}
